package com.test.base.api;

import com.test.base.dao.NideshopCollectDao;
import com.test.base.entity.NideshopCollect;
import com.test.base.utils.Assist;
import com.test.base.vo.common.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "api/collect")
@Api(description = "收藏")
public class CollectApi {
    @Autowired
    private NideshopCollectDao nideshopCollectDao;

    @ApiOperation(value = "获取收藏", notes = "")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "typeId", value = "typeId", paramType = "query", required = true, dataType = "Long")
    })
    public ResultData index(Long userId, Long typeId) {
        List<NideshopCollect> nideshopCollects = nideshopCollectDao.selectNideshopCollectByTypeId(userId, typeId);
        return ResultData.success(nideshopCollects);
    }

    @ApiOperation(value = "添加或者取消收藏", notes = "")
    @RequestMapping(value = "/addordelete", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "typeId", value = "typeId", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "valueId", value = "商品ID", paramType = "query", required = true, dataType = "Long")
    })
    public ResultData addordelete(Long userId, Long typeId, Long valueId) {
        NideshopCollect assist = new NideshopCollect();
        assist.setUserId(userId);
        assist.setValueId(valueId);
        assist.setTypeId(typeId);
        NideshopCollect collectResult = nideshopCollectDao.selectNideshopCollectByObj(assist);
        int collectRes = 0;
        String handleType = "add";
        if (null == collectResult) {
            NideshopCollect collect = new NideshopCollect();
            collect.setTypeId(typeId);
            collect.setValueId(valueId);
            collect.setUserId(userId);
            collect.setAddTime(new Date().getTime() / 1000);
            collectRes = nideshopCollectDao.insertNonEmptyNideshopCollect(collect);
        } else {
            collectRes = nideshopCollectDao.deleteNideshopCollectById(collectResult.getId());
            handleType = "delete";
        }
        if (collectRes > 0) {
            Map<String, String> map = new HashMap<>();
            map.put("type", handleType);
            return ResultData.success(map);
        } else {
            return ResultData.fail("操作失败");
        }
    }

}

